; Racal-Milgo Maxam IV                                 Version 1.00 17-Oct-1991
; (c) 1991 Hugo Fiennes

modem_initialise ()
  {
  integer oldtx=port_txspeed(),oldrx=port_rxspeed()

  port_rxclear()
  port_txspeed(9600)
  port_rxspeed(9600)

  type "AT&F"+cr
  pause(70)
  port_rxclear()

  type "ATE0Q0V1X4F0M0N1&E1&I9&K1&L0&M0"+cr
  pause(50)
  port_rxclear()

  type "AT&D2&P0&C1&S1\C5:\Q0:S57=0S2=255"+cr
  pause(50)
  port_rxclear()

  type "ATS0=0S9=1S10=1"+cr
  if (waitfor("OK",150))
    {
    port_txspeed(oldtx)
    port_rxspeed(oldrx)
    return(0)
    }

  port_txspeed(oldtx)
  port_rxspeed(oldrx)
  return(1)
  }

modem_shutdown ()
  {
  return(1)
  }

modem_connect ()
  {
  port_dtr(1)
  port_rts(1)
  type "ATO"+cr

  return(_processmessage(""))
  }

modem_disconnect ()
  {
  port_rts(0)
  port_dtr(0)
  pause 50
  port_dtr(1)
  port_rts(1)
  type cr
  }

modem_dial (string number[40],integer how)
  {
  string dialtype[1]

  port_dtr(1)
  port_rts(1)
  port_rxclear()

  if (how==0)
    {
    dialtype="P"
    }
  else
    {
    dialtype="T"
    }

  type "ATD"+dialtype+number+cr

  return(_processmessage(""))
  }

modem_answer ()
  {
  string response[30]

  type "ATS0=1"+cr

  repeat
    {
    response=$modeminput(30,modem_replywait)
    }
  until(strcmp($left(response,7),"CONNECT")==0)

  return(_processmessage(response))
  }

modem_errorcontrol (string option[10])
  {
  set(linklevel,none)

  if (comparei(option,"off"))
    {
    type("AT&E0"+cr)
    }

  if (comparei($left(option,3),"mnp"))
    {
    integer level=val($mid(option,4,1))
 
    if (level>=5)
      {
      type("AT&E1\C5"+cr)
      }
    else
      {
      type("AT&E1\C4"+cr)
      }
    }

  if (comparei(option,"vasscom"))
    {
    type("AT&E0"+cr)
    set(linklevel,vasscom)
    }

  waitfor("OK"+$chr(13)+$chr(10),100)
  pause(50)

  return
  }

modem_standard (string option[10])
  {
  if (comparei(option,"v21"))
    {
    port_txspeed(300)
    port_rxspeed(300)
    return
    }
  if (comparei(option,"v22"))
    {
    port_txspeed(1200)
    port_rxspeed(1200)
    return
    }
  if (comparei(option,"v23"))
    {
    port_txspeed(75)
    port_rxspeed(1200)
    return
    }
  if (comparei(option,"v22bis"))
    {
    port_txspeed(2400)
    port_rxspeed(2400)
    return
    }
  prints "Modem standard "+option+" is not supported."+newline
  }

_processmessage (string already[30])
  {
  string retcode[30]

  if (len(already)==0)
    {
    ; Wait for message
    retcode=$modeminput(30,modem_replywait)
    if (len(retcode)==0)
      {
      retcode=$modeminput(30,modem_replywait)
      }
    }
  else
    {
    retcode=already
    }
   
  prints retcode+newline

  ; Check for NO DIALTONE/BUSY
  switch(retcode)
    {
    case$("BUSY")
      {
      return(2)
      }
    case$("NO DIALTONE")
      {
      return(3)
      }
    }

  if (compare($left(retcode,7),"CONNECT"))
    {
    return(0)
    }

  modem_disconnect()
  return(1)
  }
